package com.frxuexi.sign.view

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.sign.SignEntity
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SignListPage(navHostController: NavHostController,vm:SignViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<SignEntity>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.addAll(it.data as Collection<SignEntity>)
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(SignIntent.getSigns(mapOf("userId" to 1,"page" to 1)))

    })
    
    Scaffold(bottomBar = {
        Button(modifier = Modifier.padding(horizontal = 20.dp).fillMaxWidth(),onClick = {
            navHostController.navigate(RoutePath.SIGN)
        }) {
            Text(text = "打卡")
        }
    }) {
        Column(Modifier.padding(it)) {
            BaseTitle(title = "考勤", navHostController = navHostController)
            Column(Modifier.padding(horizontal = 20.dp)) {
                Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
                    Text(text = "时间$week")
                    Text(text = "考勤规则")
                }
                Spacer(modifier = Modifier.height(10.dp))
                LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                    items(list){
                        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(it.signTime)
                        Text(text = time,
                            modifier = Modifier
                                .fillMaxWidth()
                                .background(colorResource(id = R.color.lightblue))
                                .padding(20.dp)
                                .combinedClickable(onLongClick = {
                                    list -= it
                                }) {
                                }
                        )
                    }
                })
            }

        }
    }
    
}